package day06;

/*
 * Author: JacksonCoder
 * FileName: ListDemo1
 * Version: 1.0.0
 * Date: 2025/4/13
 */

import java.util.*;
import java.util.stream.Collectors;

public class ListDemo1 {
    public static void main(String[] args) {

        List<String> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<String> list3 = Arrays.asList("java","python","GO");

        // add element
        list1.add("C++");
        list1.add("PHP");
        list1.add("JavaScript");

        // change element
        list1.set(0,"C/C++");

        // remove element
        list1.remove("PHP");

        //clear all element
//        list1.clear();

        // 获取元素
        String lang = list1.get(0); // 获取第一个元素
        boolean exists = list1.contains("java");  // 判断是否存在
        int index = list1.indexOf("JavaScript"); // 获取元素索引

        for(String item: list1){
            System.out.println(item);
        }

        for(int i = 0; i < list1.size(); i++){
            System.out.println(list1.get(1));
        }

        Iterator<String> it = list1.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        // use Stream
        list1.stream().forEach(System.out::println);

        // List 排序   升序/自定义排序
        List<Integer> numbers = Arrays.asList(5, 3, 9, 1);
        numbers.sort(Integer::compareTo); // 或 Collections.sort(numbers);
        //字符串按长度排序
        list1.sort(Comparator.comparing(String::length));

        // List 转数组 & 数组转 List
        String[] arr = list1.toArray(new String[0]);
        List<String> listFromArray = Arrays.asList(arr);

        // Stream 操作list
        List<String> result = list1.stream()
                .filter(s -> s.startsWith("J"))
                .map(String::toUpperCase)
                .collect(Collectors.toList());

        // 嵌套 List（List<List<T>>）
        List<List<Integer>> matrix = new ArrayList<>();

        matrix.add(Arrays.asList(1, 2, 3));
        matrix.add(Arrays.asList(4, 5, 6));

        for (List<Integer> row : matrix) {
            System.out.println(row);
        }



    }
}
